﻿using AutoStar.Common;
using AutoStar.ViewModel;
using System;
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock;
using Window = System.Windows.Window;

namespace AutoStar.Views
{
    /// <summary>
    /// WindowLayer.xaml 的交互逻辑
    /// </summary>
    public partial class WindowLayer : Window
    {
        public WindowLayer()
        {
            InitializeComponent();
            DataContext = Global.layerVM;
            Global.layerVM.CloseRequested += ViewModel_CloseRequested;
        }
        public WindowLayer(PrintLayer layer)
        {
            InitializeComponent();
            DataContext = Global.layerVM;
            //SetStyle(ControlStyles.UserPaint, true);
            //SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
            //SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
            //设置默认值
            if (layer.mode == 0)
            {
                Global.layerVM.Is1Checked = true;
                Global.layerVM.Is2Checked = false;
                Global.layerVM.Is3Checked = false;
            }
            else if (layer.mode == 1)
            {
                Global.layerVM.Is2Checked = true;
                Global.layerVM.Is1Checked = false;
                Global.layerVM.Is3Checked = false;
            }
            else if (layer.mode == 2)
            {
                Global.layerVM.Is2Checked = true;
                Global.layerVM.Is3Checked = true;
                Global.layerVM.Is1Checked = false;
            }
            //Global.layerVM.X = layer.x_start + "";
            //Global.layerVM.Y = layer.y_start + "";
            //Global.layerVM.Z = layer.z_start + "";

            //Global.layerVM.X = "0";
            //Global.layerVM.Y = "0";
            //Global.layerVM.Z = "0";
            //Global.layerVM.Width = layer.x_size + "";
            //Global.layerVM.Length = layer.y_size + "";
            //Global.layerVM.Interval = layer.interval + "";
            //Global.layerVM.Height = layer.height + "";
            //Global.layerVM.Time = layer.solidfyTime + "";
            //Global.layerVM.SelectedChannel = Global.printProject.printChannels[layer.channel].ToString();
            //Global.printLayer = layer;

            Global.layerVM.X = layer.x_start.ToString("0.00");
            Global.layerVM.Y = layer.y_start.ToString("0.00");
            Global.layerVM.Z = layer.z_start.ToString("0.00");
            Global.layerVM.Width = layer.x_size + "";
            Global.layerVM.Length = layer.y_size + "";
            Global.layerVM.Interval = layer.interval + "";
            Global.layerVM.Height = layer.height + "";
            Global.layerVM.Time = layer.solidfyTime + "";
            Global.layerVM.SelectedChannel = Global.layerVM.PrintChannels[layer.channel];
            //Global.layerVM.SelectedChannel = Global.printProject.printChannels[layer.channel].ToString();
            Global.printLayer = layer;

            if (!Global.SliceVM.IsEnbaled)
            {
                if (Global.printLayer.mode >= 10)
                {
                    X_Enabled.IsEnabled = false;
                    Y_Enabled.IsEnabled = false;
                    W_Enabled.IsEnabled = false;
                    L_Enabled.IsEnabled = false;
                    I_Enabled.IsEnabled = false;
                    H_Enabled.IsEnabled = false;
                }
            }
            else
            {   //编辑所有层 除XY全禁用
                Z_Enabled.IsEnabled = false;
                W_Enabled.IsEnabled = false;
                L_Enabled.IsEnabled = false;
                I_Enabled.IsEnabled = false;
                H_Enabled.IsEnabled = false;
                T_Enabled.IsEnabled = false;
                //C_Enabled.IsEnabled = false;
            }
            //原winform需求 禁用部分控件,只保留Z 时间 打印通道下拉 三个模式
        }
        private void ViewModel_CloseRequested(object sender, EventArgs e)
        {

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (Global.printLayer.mode >= 10)//如果是通过分层切片方式自动添加的打印层
            {
                //修改层高
                for (int i = 0; i < Global.printLayer.points.Count; i++)
                {
                    Global.printLayer.points[i].Z = float.Parse(Global.layerVM.Z);
                }
                Global.printLayer.z_start = float.Parse(Global.layerVM.Z);
                //修改固化时间
                Global.printLayer.solidfyTime = int.Parse(Global.layerVM.Time);
                //修改通道
                //if (Global.layerVM.SelectedChannel == "通道1")
                //    Global.printLayer.channel = 0;
                //else if (Global.layerVM.SelectedChannel == "通道2")
                //    Global.printLayer.channel = 1;
                //else if (Global.layerVM.SelectedChannel == "通道3")
                //    Global.printLayer.channel = 2;
                //else if (Global.layerVM.SelectedChannel == "通道4")
                //    Global.printLayer.channel = 3;
                //else if (Global.layerVM.SelectedChannel == "通道5")
                //    Global.printLayer.channel = 4;
                Global.printLayer.channel = GetChannelIndex(Global.layerVM.SelectedChannel);
                AddLayerEvent(Global.printLayer);
            }
            else
            {
                int index = GetChannelIndex(Global.layerVM.SelectedChannel);
                var bPosition = Global.printProject.printChannels[index].position;
                float pX = float.Parse(Global.layerVM.X);
                float pY = float.Parse(Global.layerVM.Y);
                if (Global.layerVM.Is1Checked)
                    Global.layerVM.Tag = 0;
                if (Global.layerVM.Is2Checked)
                    Global.layerVM.Tag = 1;
                if (Global.layerVM.Is3Checked)
                    Global.layerVM.Tag = 2;
                //if (bPosition.X > pX)
                //    pX += bPosition.X;
                //if (bPosition.Y > pY)
                //    pY += bPosition.Y;
                AddLayerEvent(new PrintLayer(Global.layerVM.Tag, index,
                float.Parse(Global.layerVM.Interval), float.Parse(Global.layerVM.Width),
                float.Parse(Global.layerVM.Length), pX, pY,
                float.Parse(Global.layerVM.Z),
                int.Parse(Global.layerVM.Time), float.Parse(Global.layerVM.Height)));

                //AddLayerEvent(new PrintLayer(Global.layerVM.Tag, index,
                //float.Parse(Global.layerVM.Interval), float.Parse(Global.layerVM.Width),
                //float.Parse(Global.layerVM.Length),
                //Global.printProject.printChannels[index].position.X + float.Parse(Global.layerVM.X),
                //Global.printProject.printChannels[index].position.Y + float.Parse(Global.layerVM.Y),
                //float.Parse(Global.layerVM.Z),
                //int.Parse(Global.layerVM.Time), float.Parse(Global.layerVM.Height)));

                //AddLayerEvent(new PrintLayer(Global.layerVM.Tag, GetChannelIndex(Global.layerVM.SelectedChannel),
                //float.Parse(Global.layerVM.Interval), float.Parse(Global.layerVM.Width),
                //float.Parse(Global.layerVM.Length), float.Parse(Global.layerVM.X),
                //float.Parse(Global.layerVM.Y), float.Parse(Global.layerVM.Z),
                //int.Parse(Global.layerVM.Time), float.Parse(Global.layerVM.Height)));
            }
            this.Close();
        }
        private int GetChannelIndex(string channel)
        {
            if (Global.layerVM.SelectedChannel == "通道1")
                return 0;
            else if (Global.layerVM.SelectedChannel == "通道2")
                return 1;
            else if (Global.layerVM.SelectedChannel == "通道3")
                return 2;
            else if (Global.layerVM.SelectedChannel == "通道4")
                return 3;
            else if (Global.layerVM.SelectedChannel == "通道5")
                return 4;
            else if (Global.layerVM.SelectedChannel == "通道6")
                return 5;
            else if (Global.layerVM.SelectedChannel == "通道7")
                return 6;
            else
                return 0;
        }
        //声明委托
        public delegate void LayerDelegate(PrintLayer layerChanged);
        //声明事件
        public event LayerDelegate AddLayerEvent;

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void RadioButton1_Click(object sender, RoutedEventArgs e)
        {
            Global.layerVM.Tag = 0;
        }

        private void RadioButton2_Click(object sender, RoutedEventArgs e)
        {
            Global.layerVM.Tag = 1;
        }
        private void RadioButton3_Click(object sender, RoutedEventArgs e)
        {
            Global.layerVM.Tag = 2;
        }
    }
}
